package com.jz.community.moduleauthorization.login.presenter;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.basecomm.mvp.BaseLifeCyclePresent;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleauthorization.login.model.LoginModel;
import com.jz.community.moduleauthorization.login.model.LoginModelImpl;
import com.jz.community.moduleauthorization.login.ui.LoginView;
import com.jz.community.moduleauthorization.utils.SpUtils;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/3/20 14:03
 */
public class LoginPresenter extends BaseLifeCyclePresent<LoginView.View> implements LoginView.Presenter {
    private LoginModel loginModel;
    private LoginView.View loginView;

    public LoginPresenter(LoginView.View view) {
        loginView = view;
        loginModel = new LoginModelImpl(view.getContext());
    }

    @Override
    public void setPresenter(LoginView.View v) {
        super.setPresenter(v);
    }

    @Override
    public void getLoginInfo(String mobi, String password, String loginType, String params, String deviceId, String registrationId) {

        saveLoginType(loginType);

        loginModel.requestLoginInfo(mobi, password, loginType, params, deviceId, registrationId, new OnLoadListener<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo loginInfo) {
                loginView.showLoginInfo(loginInfo);
            }

            @Override
            public void onFail(String s, int code) {

            }
        });
    }

    private void saveLoginType(String loginType) {
        SpUtils.getInstance().setLoginType(loginView.getContext(), loginType);
    }

    @Override
    public void getLoginSmsCode(String type, String mobi) {
        loginModel.requestGetSmsCode(type, mobi, new OnLoadListener<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo baseResponseInfo) {
                loginView.showSmsCodeInfo(baseResponseInfo);
            }

            @Override
            public void onFail(String s, int code) {

            }
        });
    }

    @Override
    public void getResetPwdInfo(HashMap<String, Object> map) {
        loginModel.requestGetResetPwd(map, new OnLoadListener<BaseResponseInfo>() {
            @Override
            public void onSuccess(BaseResponseInfo userInfo) {
                loginView.showResetPwdInfo(userInfo);
            }

            @Override
            public void onFail(String s, int code) {
                if (!Preconditions.isNullOrEmpty(s)) {
                    WpToast.l(loginView.getContext(), s);
                    loginView.showError();
                }
            }
        });
    }

    @Override
    public void getBindJupsh(HashMap<String, Object> map) {
        loginModel.requestBindJupsh(map);
    }
}
